package com.example.kangjujiaofei.controller;


import com.example.kangjujiaofei.contants.ApiResponse;
import com.example.kangjujiaofei.entity.CreateDate;
import com.example.kangjujiaofei.service.BunissesService;
import com.example.kangjujiaofei.service.XinShiChuanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.text.ParseException;

@RestController
@Slf4j
@RequestMapping("/xsc")
public class XinShiChuanController {


    @Autowired
    private BunissesService bunissesService;

    @Autowired
    private XinShiChuanService xinShiChuanService;


    /**
     * 获取对应手机号待付款物业费
     * @param
     * @return
     */
    @GetMapping("/getLoginCode")
    public ApiResponse getLoginCode(){
        String str = xinShiChuanService.getNWExID();
        return ApiResponse.success(str);
    }

    /**
     * 获取对应手机号待付款物业费
     * @param phone
     * @return
     */
    @GetMapping("/getNeedPay")
    @ResponseBody
    public ApiResponse getNeedPay(@RequestParam(value = "phone") String phone){
        try {
            return bunissesService.getUserBill(phone);
        } catch (ParseException e) {
            e.printStackTrace();
            return ApiResponse.fail(e.getMessage(),"error");
        }
    }

//    /**
//     * 下单支付
//     * @param phone
//     * @return
//     */
//    @GetMapping("/payBill")
//    public ApiResponse payBill(@RequestParam(value = "phone") String phone){
//        try {
//            return bunissesService.getUserBill(phone);
//        } catch (ParseException e) {
//            e.printStackTrace();
//            return ApiResponse.fail(e.getMessage(),"error");
//        }
//    }

    /**
     * 下单支付
     * @param list
     * @return
     */
    @PostMapping("/createPay")
    public ApiResponse createPay(@RequestBody CreateDate list){
        if (list.getList()==null||list.getList().isEmpty()){
            return  ApiResponse.ERROR;
        }
        return bunissesService.createPay(list.getList());
//        try {
//            return bunissesService.getUserBill(phone);
//        } catch (ParseException e) {
//            e.printStackTrace();
//            return ApiResponse.fail(e.getMessage(),"error");
//        }
    }

    /**
     * 生成微信支付订单
     * @return
     */
    @PostMapping("/wxPay")
    @ResponseBody
    public ApiResponse pay(@RequestParam(value = "packageId") String packageId,@RequestParam(value = "phone",required = false) String phone,@RequestParam(value = "openid") String openid){
        try {
            log.error("non"+openid);
            return bunissesService.payBill(packageId,openid);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.fail(e.getMessage(),e.getMessage());
        }
    }


    @GetMapping("/onlyPay")
    @ResponseBody
    public ApiResponse onlyPay(@RequestParam(value = "payNo") String payNo,@RequestParam(value = "houseId") String houseId){
        return bunissesService.onlyGetNeedPay(houseId,payNo);
    }

}
